#include "metaitem.h"
#include "metadata.h"


MetaItem::MetaItem(MetaData *parent) :
    QObject(parent),
    d(new Data())
{
    d->editType = EditType_SINGLE;
}

MetaItem::MetaItem(const QString &field, MetaData *parent) :
    QObject(parent),
    d(new Data())
{
    d->field = field;
    d->editType = EditType_SINGLE;
}

MetaItem::MetaItem(const QString &optionRef, const QString &field, MetaData *parent) :
    QObject(parent),
    d(new Data())
{
    d->optionRef = optionRef;
    d->field = field;
    d->editType = EditType_SINGLE;
}

MetaItem::MetaItem(const MetaItem &other) :
    QObject(other.parent()),
    d(other.d)
{
}

QString MetaItem::field() const
{
    return d->field;
}

void MetaItem::setField(const QString &field)
{
    d->field = field;
}

int MetaItem::index() const
{
    MetaData* data = qobject_cast<MetaData*>(parent());
    if (!data) return -1;

    return data->indexOf(const_cast<MetaItem*>(this));
}

void MetaItem::setIndex(int index)
{
    d->index = index;

    if (d->index < 0) return;

    MetaData* data = qobject_cast<MetaData*>(parent());
    if (!data) return;

    int oldIndex = data->indexOf(const_cast<MetaItem*>(this));
    if (oldIndex < 0)
    {
        data->insertItem(d->index, const_cast<MetaItem*>(this));
        return;
    }

    data->moveItem(oldIndex, d->index);
}

QString MetaItem::optionRef() const
{
    return d->optionRef;
}

void MetaItem::setOptionRef(const QString &options)
{
    d->optionRef = options;
}

int MetaItem::editType() const
{
    return d->editType;
}

void MetaItem::setEditType(int editType)
{
    d->editType = editType;
}

bool MetaItem::editable() const
{
    return d->editable;
}

void MetaItem::setEditable(bool enable)
{
    d->editable = enable;
}

void MetaItem::setParent(QObject *parent)
{
    QObject::setParent(parent);

    setIndex(d->index);
}


